package com.example.temporalencrypter;

import java.io.File;
import java.util.Calendar;

import com.ipaulpro.afilechooser.utils.FileUtils;

import encrypter.Encrypter;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

public class EncryptActivity extends Activity {
	private static final int REQUEST_CHOOSER = 1234;
	File fileSelected;
	private Calendar cal;
	private int day;
	private int month;
	private int year;
	private int hour;
	private int min;
	private String fullDate;
	private boolean dateSet = false;
	private boolean timeSet = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_encrypt);
		cal = Calendar.getInstance();
		

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (requestCode) {
		case REQUEST_CHOOSER:
			if (resultCode == RESULT_OK) {

				final Uri uri = data.getData();

				String path = FileUtils.getPath(this, uri);

				fileSelected = FileUtils.getFile(this, uri);
				if (path != null && FileUtils.isLocal(path)) {
					fileSelected = new File(path);
				}

			}

			break;
		}
		TextView txtView1 = (TextView) findViewById(R.id.selectedFileText);
		if (fileSelected != null) {
			txtView1.setText(fileSelected.getPath());
		}

	}

	@Override
	protected void onStart() {
		super.onStart();
		Button btChooseFile = (Button) findViewById(R.id.chooseFileButton);
		Button btDoEncrypt = (Button) findViewById(R.id.doEncryptButton);
		Button btChooseDate = (Button) findViewById(R.id.chooseDateButton);
		Button btChooseTime = (Button) findViewById(R.id.chooseTimeButton);
		btChooseFile.setOnClickListener(new View.OnClickListener() {

			public void onClick(View arg0) {
				
				Toast.makeText(getApplicationContext(),
				
						"Seleccione el archivo", Toast.LENGTH_SHORT).show();

				Intent getContentIntent = FileUtils.createGetContentIntent();

				Intent intent = Intent.createChooser(getContentIntent,
						"Select a file");
				startActivityForResult(intent, REQUEST_CHOOSER);

			}
		});
		
		btChooseDate.setOnClickListener(new View.OnClickListener() {

			public void onClick(View arg0) {
				showDialog(0);

			}
		});
		
		btChooseTime.setOnClickListener(new View.OnClickListener() {

			public void onClick(View arg0) {
				showDialog(1);

			}
		});
		btDoEncrypt.setOnClickListener(new View.OnClickListener() {

			public void onClick(View arg0) {
				
				File directory = new File(Environment.getExternalStorageDirectory()+File.separator+"encriptedFiles");
				if(!directory.exists()){
					Toast.makeText(getApplicationContext(),
							"Carpeta "+ directory.getAbsolutePath() + " generada para guardar archivos encriptados", Toast.LENGTH_SHORT).show();
					directory.mkdirs();
				}
				
				if(timeSet && (fileSelected!= null) && dateSet && (cal.after(Calendar.getInstance()))){
					
					 RadioButton rsobreescribir=(RadioButton)findViewById(R.id.radioSaveInEcryptedFiles);
					 RadioButton  rCarpetaDedicada=(RadioButton)findViewById(R.id.radioSaveSamePlace);
					if(rCarpetaDedicada.isChecked()){
						System.out.println("Se guarda en carpeta dedicada");
						File newFile = new File(directory
								+ "/encrypted"+fileSelected.getName());
						Encrypter.get().encrypt(fileSelected, cal,newFile);
						Toast.makeText(getApplicationContext(),
								"Archivo encriptado! -> "+ newFile, Toast.LENGTH_SHORT).show();
					}else{
						if(rsobreescribir.isChecked()){
							System.out.println("se sobreescribe en carpeta");
							Encrypter.get().encrypt(fileSelected, cal);
							Toast.makeText(getApplicationContext(),
									"Archivo sobreescrito y encriptado!", Toast.LENGTH_SHORT).show();
						}
					}
						
						

					
						
					
					
					
				}else{
					Toast.makeText(getApplicationContext(),
							"Por favor seleccione el archivo, y una fecha y hora posterior a la actual", Toast.LENGTH_SHORT).show();
				}

				

			}
		});

	}
	
	@Override
	@Deprecated
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case 0:
			year = cal.get(Calendar.YEAR);
			month = cal.get(Calendar.MONTH);
			day = cal.get(Calendar.DAY_OF_MONTH);
			return new DatePickerDialog(this, datePickerListener, year, month, day);
		
		default:
			hour = cal.get(Calendar.HOUR_OF_DAY);
			min = cal.get(Calendar.MINUTE);
			return new TimePickerDialog(this, timePickerListener, hour, min, true);


		}
		
	}
	

	private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
		public void onDateSet(DatePicker view, int selectedYear,
				int selectedMonth, int selectedDay) {
			
			cal.set(selectedYear, selectedMonth, selectedDay);
			fullDate = selectedDay + " / " + (selectedMonth + 1) + " / "
					+ selectedYear + " ---";

			TextView txtView = (TextView) findViewById(R.id.selectedDateText);
			if (fullDate != null) {
				txtView.setText(fullDate);
			}
			dateSet = true;
		}
	};
	
	protected Dialog createTimeDialog(int id) {
		return new TimePickerDialog(this, timePickerListener, hour, min, true);
	}
	
	private TimePickerDialog.OnTimeSetListener timePickerListener = new TimePickerDialog.OnTimeSetListener() {
		@Override
		public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
			cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), hourOfDay, minute);
		

			TextView txtView = (TextView) findViewById(R.id.selectedTimeText);
			txtView.setText(hourOfDay + ":"+minute);
			timeSet = true;
		}
	};

}
